package com.ibm.spatiotemp.datamodel.config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;

public class CacheServerManager {
	public static Logger logger = Logger.getLogger(CacheServerManager.class);
	
	/*for later to support multiple topics*/
//	public static MetaVector<CacheServerMeta>
//	servervector = new MetaVector<CacheServerMeta>(MetaVector.DefaultMaxSize);
	
	public static Shard<CacheServerMeta> servervector = new Shard<CacheServerMeta>();

	private static void InsertCacheServer(String line) {
		CacheServerMeta curserv = new CacheServerMeta(line);
		servervector.addNode(curserv);
	}

	public static void LoadfromFile(String filepath) {
		BufferedReader freader = null;
		try {
			freader = new BufferedReader(new FileReader(filepath));
			String curline = null;
			while ((curline = freader.readLine()) != null) {
				InsertCacheServer(curline);
			}
		} catch (FileNotFoundException e) {
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		}
	}
	
	public static void init(String configpath){
		LoadfromFile(configpath);
	}
	
//	public static CacheServerMeta getCacheServerMeta_Random(){
//		String randomkeystring = System.currentTimeMillis()+"";
//		CacheServerMeta res = servervector.keyToNode(randomkeystring);
//		if(res == null){
//			logger.error("error to get a redis server meta");
//		}
//		return res;
//	}
	
	/**
	 * get a redis server to store the path's file, key is the path
	 * @param pathaskey
	 * @return
	 */
	public static CacheServerMeta getCacheServerMetaByPathKey(String pathaskey){
//	String randomkeystring = System.currentTimeMillis()+"";
	CacheServerMeta res = servervector.keyToNode(pathaskey);
	if(res == null){
		logger.error("error to get a redis server meta");
	}
	return res;
}
	
}
